<?php
namespace app\admin\controller;

use \think\Validate;
use \think\Config;
class Project extends Base{
    
    public function index(){
        $get = $this->req->get();
        $rule = [
            'page' => 'require'
        ];
        $validate = new Validate($rule);
        $model = new \app\admin\model\Project;
        if($validate->check($get)){
            $page = $get['page'];
        }else{
            $page = 0;
        }
        $rows = Config::get('paginate.list_rows');
        $list = $model->paginate($rows);
        $data = $model->limit($page*$rows,$rows)->select();
        $this->assign('list',$list);
        $this->assign('data',$data);
        return $this->fetch();
    }

    public function add(){
        return $this->fetch();
    }

    /**
     * @name insert
     * @return json
     * @author Aninet Thu Jul  5 10:44:25 2018
     */
    public function insert(){
        $post = $this->req->post();
        $rule = [
            'name' => 'require',
            'content' => 'require',
        ];
        $validate = new Validate($rule);
        $data = [];
        if($validate->check($post)){
            foreach($post as $key=>$p){
                $data['pro_'.$key] = $p;
            }
            $model = new \app\admin\model\Project;
            if($model->save($data)){
                $info = [
                    'msg' => '添加成功'
                ];
                echo $this->right($info);
            }else{
                $info = [
                    'msg' => '添加失败'
                ];
                echo $this->wrong($info);
            }
        }else{
            $info = [
                'msg' => '请把所需数据填写完整'
            ];
            echo $this->wrong($info);
        }
        return;
    }

    /**
     * 项目修改页面
     * 
     * @return html page
     * @date Thu Jul  5 20:25:35 2018
     */
    public function edit(){
        $get = $this->req->get();
        $rule = [
            'id' => 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($get)){
            $model = new \app\admin\model\Project;
            $data = $model->get($get['id']);
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            return $this->error('数据出错');
        }
    }

    /**
     * 删除项目
     * 
     * @return json 
     * @date Fri Jul  6 09:46:12 2018
     */
    public function remove(){
        $post = $this->req->post();
        $rule = [
            'id' => 'require'
        ];
        $validate = new Validate($rule);
        if($validate->check($post)){
            $model = new \app\admin\model\Project;
            $re = $model->destroy($post['id']);
            if($re){
                $info  = [
                    'msg' => '删除成功'
                ];
                echo $this->right($info);
            } else {
                $info  = [
                    'msg' => '删除失败'
                ];
                echo $this->wrong($info);
                
            }
        }else{
                $info  = [
                    'msg' => '没有数据'
                ];
                echo $this->wrong($info);
        }
        return;
    }
}
